//
//  CityManager.m
//  LzzizDemo
//
//  Created by xbnzd on 16/11/8.
//  Copyright © 2016年 xbnzd. All rights reserved.
//

#import "CityManager.h"
#import "ProvinceModel.h"
#import "CitysModel.h"



@implementation CityManager


/**
 返回plist中的原始数组数据

 */
+ (NSMutableArray *)reurnCityListArr
{
//    NSString *str = [CityManager createPlist];
    NSString *str = [[NSBundle mainBundle]pathForResource:@"city" ofType:@"plist"];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:str];
    
    return arr;
}


/**
 返回模型数组,直接用在城市列表

 */
+ (NSArray *)returnDataArrForTableView
{
   NSMutableArray *mutableArr = [[NSMutableArray alloc] init];
    
    NSMutableArray *arrTemp = [self reurnCityListArr];
    for (int i = 0; i < arrTemp.count; i ++) {
        NSDictionary *dic = arrTemp[i];
        ProvinceModel *m = [[ProvinceModel alloc] init];
        m.enName = dic[@"enName"];
        m.arName = dic[@"arName"];
        m.provinceNO = dic[@"province"];
        NSMutableArray *provinceCityArr = [[NSMutableArray alloc] init];
        NSArray *cityArrTemp = dic[@"cityCode"];
        for (int j = 0; j < cityArrTemp.count; j ++) {
            NSDictionary *cityDic = cityArrTemp[j];
            CitysModel *cityM = [[CitysModel alloc] init];
            cityM.enName = cityDic[@"enName"];
            cityM.arName = cityDic[@"arName"];
            cityM.cityNO = cityDic[@"city"];
            [provinceCityArr addObject:cityM];
        }
        m.citysArr = [provinceCityArr copy];
        
        [mutableArr addObject:m];
    }
        
    
    return [mutableArr copy];
}



/**
 返回用于搜索联想的数据数组

 */
+ (NSArray *)returnDataArrForSearchRemind
{
   NSMutableArray *mutableArr = [[NSMutableArray alloc] init];
    
    NSMutableArray *arrTemp = [self reurnCityListArr];
    for (int i = 0; i < arrTemp.count; i ++) {
        NSDictionary *dic = arrTemp[i];
        NSMutableArray *provinceCityArr = [[NSMutableArray alloc] init];
        NSArray *cityArrTemp = dic[@"cityCode"];
        for (int j = 0; j < cityArrTemp.count; j ++) {
            NSDictionary *cityDic = cityArrTemp[j];
            CitysModel *cityM = [[CitysModel alloc] init];
            cityM.enName = cityDic[@"enName"];
            cityM.arName = cityDic[@"arName"];
            cityM.cityNO = cityDic[@"city"];
            [provinceCityArr addObject:cityM];
            
            [mutableArr addObject:cityM];
        }
        
        
    }
        
    
    return [mutableArr copy];

}


/**
 根据城市名获取城市编码
 
 @param cityNO 城市名字
 @return <#return value description#>
 */
+ (NSString *)returnTheCityNameWithCityCode:(NSString *)cityNO
{
    NSPredicate *p;
    
    p = [NSPredicate predicateWithFormat:@"cityNO CONTAINS[cd] %@",cityNO];

    NSArray *cityModesArr = [CityManager returnDataArrForSearchRemind];
    
    NSArray *arr = [cityModesArr filteredArrayUsingPredicate:p];
    if (arr.count > 0) {
        CitysModel *m = arr[0];
        return m.name;
    }
    
    //如果查询失败，返回默认的北京编码
    return @"北京";

}


@end
